#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#define LED 2

const char* ssid = "ChinaNet-******";
const char* password = "133********";

const char* host = "192.168.5.8";
const int httpsPort = 10101;
const String url = "/eops/iot/heart?request=test";

void smartConfig()
{
  WiFi.mode(WIFI_STA);
  Serial.println("\r\nWait for connect");
  WiFi.beginSmartConfig();
  while (1)
  {
    Serial.print(".");
    digitalWrite(LED, LOW);
    delay(500);
    digitalWrite(LED, HIGH);
    delay(500);
    if (WiFi.smartConfigDone())
    {
      Serial.println("SmartConfig Success");
      Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
      Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
      break;
    }
  }
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Start module");
  pinMode(LED, OUTPUT);
  digitalWrite(LED, 0);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  //  smartConfig();
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(1000);
  //  Serial.println("Start module");

  WiFiClient client;
  if (!client.connect(host, httpsPort)) {
    Serial.println("connection failed");
    return;
  }
  delay(10);
  String postRequest = (String)("GET ") + url + "/ HTTP/1.1\r\n" +
                       "Content-Type: text/html;charset=utf-8\r\n" +
                       "Host: " + host + "\r\n" +
                       "User-Agent: BuildFailureDetectorESP8266\r\n" +
                       "Connection: Keep Alive\r\n\r\n";
  Serial.println(postRequest);
  client.print(postRequest);
  String line = client.readStringUntil('\n');
  while (line.length() != 0) {
    Serial.println(line);
    line = client.readStringUntil('\n');
  }
  Serial.println(line);
  client.stop();
  delay(3000);
}
